#!/usr/bin/php5
<?php
//------------------------------------------------------------------
/// \file   daemon.php
/// \brief  Radio maintenance daemon
/// \author A. M. Soell
/// \date   4/21/2008
/// \version  3.0
///
/// Cron script which monitors the Shoutcast process and performs
/// cleanup on already-played tracks and queues up new tracks to
/// fill empty slots
///
// $Id:$
//-------------------------------------------------------------------

// We don't want errors clogging up the root mailbox
//  error_reporting(1);

  require_once('/srv/radio/configuration.php');
  $config = new Config();
  require_once($config->site_root.'/include/framework.php');
  
  $q = new Queue();
  $c = new Catalog();
  $filters = Filter::getFilters(FILTER_RANDOM);  
  
  // To wipe out any queued up tracks and add a fresh batch of random tracks to the queue, use 
  // the '-flush' paramter
  if (in_array('-flush', $argv)) $q->flush();
  $q->updateQueue();

  while ($q->getSize()<$config->min_queue_length) {
    $r = new Request($c->getRandomTrack($filters));
    $q->enqueue($r, false);
  }

  // To force through multiple random tracks, use the '-force n' syntax, where n is the number of
  // random tracks to add to the queue
  if ((($pos = array_search('-force', $argv)) !== false) && (is_numeric($argv[$pos+1]))) {
    for ($i=0; $i<$argv[$pos+1]; $i++) {
      $r = new Request($c->getRandomTrack($filters));
      $q->enqueue($r, false);
    }
  }
  
  $q->writeQueue();
  $q->updateServer();
  
  unset($q, $c, $config);
?>
